package day1;

import com.sc1.mapper.EUserMapper;
import com.sc1.pojo.EmpUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class TsetMybatis {
    static Scanner sc=new Scanner(System.in);
    static InputStream is;
    static {
        try {
            is = Resources.getResourceAsStream("mybatis.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    static SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
    static SqlSession session = sf.openSession();
    static EUserMapper mapper = session.getMapper(EUserMapper.class);

    @Test
    public void select() throws IOException {
        List<EmpUser> list= mapper.select();
        for(EmpUser user:list){
            System.out.println(user.getId());
            System.out.println(user.getUsername());
            System.out.println(user.getPassword());
            System.out.println(user.getDept());
            System.out.println(user.getJob());
            System.out.println(user.getSal());
        }
        session.commit();
        session.close();
    }

    @Test
    public void delete() throws IOException {
        Integer id = 2;
        mapper.delete(id);
        session.commit();
        session.close();
    }

    @Test
    public void insert() throws IOException {
        mapper.insert(new EmpUser("王五","123","Software Engineer","Development",5000));
        session.commit();
        session.close();
    }

    @Test
    public void update() throws IOException {
        Integer id = 3;
        mapper.update(id);
        session.commit();
        session.close();
    }
}
